#include "DBControl.h"
#include <exception>
#include <vector>
#include <array>

#include <QSharedPointer>
#include <QVector>
#include <QMap>

#include "ConnMySqHelper.h"


void DBControl::createBcweldingDB()
{
	QSharedPointer<ConnMySqHelper> connMySqHelper = ConnMySqHelper::GetThreadMySqlHelp();
	if (connMySqHelper == NULL)
		return;

	QString tableName = TABLENAME;
	QString constructSentence("CREATE TABLE IF NOT EXISTS `%1` (\
			`id` int(11) NOT NULL AUTO_INCREMENT,\
			`barcode` varchar(256) DEFAULT NULL,\
			`uid` varchar(256) DEFAULT NULL,\
			`data_time` datetime  DEFAULT NULL,\
			`upload` tinyint(4) DEFAULT '0',\
			`message` varchar(512) DEFAULT NULL,\
			PRIMARY KEY(`id`),\
			INDEX `datatimeIndex` (`data_time`) USING BTREE,\
			INDEX `barcodeIndex` (`barcode`) USING BTREE,\
			INDEX `uploadIndex` (`upload`) USING BTREE,\
			INDEX `uidIndex` (`uid`) USING BTREE\
			) ENGINE = InnoDB DEFAULT CHARSET = utf8; ");

	if (!connMySqHelper->local_createSiteDataBase(tableName, constructSentence))
	{
		qInfo() << __FUNCTION__ << constructSentence << endl;
	}
}

bool DBControl::insertData(QSharedPointer<BasicDetectionDefine2> Data)
{
	if (Data == NULL)
		return false;

	QSharedPointer<ConnMySqHelper> connMySqHelper = ConnMySqHelper::GetThreadMySqlHelp();
	if (connMySqHelper == NULL)
		return false;

	bool nRes = true;

	QString sqlcmd = QString("insert into %1\
		(barcode,data_time,upload,uid,message\
		)\
		values(\
		'%2'  , '%3'  , %4 , '%5', '%6'\
		)")
		.arg(TABLENAME)
		.arg(Data->barcode)
		.arg(Data->date_time)
		.arg(0)
		.arg(Data->uid)
		.arg(Data->message);

	if (!connMySqHelper->execSqlCmd(sqlcmd))
	{
		qInfo() << __FUNCTION__ << sqlcmd << endl;
		nRes = false;
	}
	return nRes;
}

bool DBControl::updateData(QSharedPointer<BasicDetectionDefine2> Data)
{
	if (Data == NULL)
		return false;

	QSharedPointer<ConnMySqHelper> connMySqHelper = ConnMySqHelper::GetThreadMySqlHelp();
	if (connMySqHelper == NULL)
		return false;

	QString sqlCmd = QString("update %1 set upload = %2,message='%3'  where barcode = '%4' and uid = '%5' ")
		.arg(TABLENAME)
		.arg(Data->upload)
		.arg(Data->message)
		.arg(Data->barcode)
		.arg(Data->uid);

	bool nRes = connMySqHelper->execSqlCmd(sqlCmd);
	if (nRes == false)
	{
		qInfo() << __FUNCTION__ << " " << sqlCmd << endl;
	}

	return nRes;
}

std::vector<QSharedPointer<BasicDetectionDefine2>> DBControl::getData(bool isUpload, int num)
{
	std::vector<QSharedPointer<BasicDetectionDefine2>> result;

	QSharedPointer<ConnMySqHelper> connMySqHelper = ConnMySqHelper::GetThreadMySqlHelp();
	if (connMySqHelper == NULL)
		return result;

	QVector<QSqlRecord> last_query_result;
	bool isOK = false;
	QString sqlCmds = QString("select * from %1 where upload = %2 order by id desc limit %3")
		.arg(TABLENAME)
		.arg(isUpload ? 1 : 0)
		.arg(num);
	if (connMySqHelper->select(sqlCmds, last_query_result))
	{
		foreach(const QSqlRecord & record, last_query_result)
		{
			QSharedPointer<BasicDetectionDefine2> data = QSharedPointer<BasicDetectionDefine2>::create();
			data->id = record.value(record.indexOf("id")).toInt();
			data->barcode = record.value(record.indexOf("barcode")).toString();
			data->date_time = record.value(record.indexOf("data_time")).toDateTime().toString("yyyy-MM-dd HH:mm:ss");
			data->upload = record.value(record.indexOf("upload")).toInt();
			data->message = record.value(record.indexOf("message")).toString();
			data->uid = record.value(record.indexOf("uid")).toString();
			result.push_back(data);
		}
		isOK = true;
	}
	return result;
}
